package com.zc.controller;

import com.github.pagehelper.PageInfo;
import com.zc.annotation.SystemLogAnnotation;
import com.zc.pojo.Customer;
import com.zc.service.CustomerService;
import com.zc.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @SystemLogAnnotation("客户信息跳转列表页面")
    @GetMapping("toList")
    public String toList() {
        return "customer/customerList";
    }


    @SystemLogAnnotation("客户信息列表查询")
    @GetMapping("list")
    @ResponseBody
    public JSONResult list(String realName, String phone, Integer page, Integer limit) {
        Map<String, Object> data = new HashMap<>();

        PageInfo<Customer> pageInfo = customerService.queryByPage(realName, phone, page, limit);

        List<Customer> customerList = pageInfo.getList();

        data.put("customerSize", pageInfo.getTotal());
        data.put("customers", customerList);
        return JSONResult.ok(data);
    }

    @SystemLogAnnotation("客户信息跳转新增页面")
    @GetMapping("toAdd")
    public String toAdd() {
        return "customer/customerAdd";
    }

    @SystemLogAnnotation("客户信息新增")
    @PostMapping("add")
    @ResponseBody
    public JSONResult Add(@RequestBody Customer customer) {

        customerService.save(customer);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("客户信息跳转修改页面")
    @GetMapping("toUpDate/{customerId}")
    public String toUpData(@PathVariable Long customerId, Model model) {
        Customer customer = customerService.getById(customerId);
        model.addAttribute("customer", customer);
        return "customer/customerUpDate";
    }

    @SystemLogAnnotation("客户信息修改")
    @PutMapping("update")
    @ResponseBody
    public JSONResult upDate(@RequestBody Customer customer) {

        customerService.update(customer);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("客户信息删除")
    @DeleteMapping("delete/{customerId}")
    @ResponseBody
    public JSONResult delete(@PathVariable Long customerId) {

        customerService.logicalDelete(customerId);

        return JSONResult.ok();
    }

    @SystemLogAnnotation("客户信息跳转详情页面")
    @GetMapping("toDetail/{customerId}")
    public String toDetail(@PathVariable Long customerId, Model model) {
        Customer customer = customerService.getById(customerId);
        model.addAttribute("customer", customer);
        return "customer/customerDetail";
    }
}